﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Sora.HelpSystem.Package;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace Sora.HelpSystem.UI.WPF
{
    public class HelpDocumentSource : DependencyObject
    {
        public HelpDocumentProvider DocumentProvider
        {
            get { return (HelpDocumentProvider)GetValue(DocumentProviderProperty.DependencyProperty); }
            private set { SetValue(DocumentProviderProperty, value); }
        }

        public static readonly DependencyPropertyKey DocumentProviderProperty =
            DependencyProperty.RegisterReadOnly("DocumentProvider", typeof(HelpDocumentProvider), typeof(HelpDocumentSource), new UIPropertyMetadata(null));

        public HelpDocument Document
        {
            get { return (HelpDocument)GetValue(DocumentProperty.DependencyProperty); }
            private set { SetValue(DocumentProperty, value); }
        }

        public static readonly DependencyPropertyKey DocumentProperty =
            DependencyProperty.RegisterReadOnly("Document", typeof(HelpDocument), typeof(HelpDocumentSource), new UIPropertyMetadata(null));

        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(string), typeof(HelpDocumentSource), new UIPropertyMetadata(SourcePropertyChanged));

        private static void SourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var hSource = sender as HelpDocumentSource;

            if (hSource != null)
            {
                var new_value = e.NewValue as string;

                if (String.IsNullOrEmpty(new_value))
                {
                    hSource.Document = null;
                    hSource.DocumentProvider = null;
                }
                else
                {
                    try
                    {
                        if (!Path.IsPathRooted(new_value))
                        {
                            if (!File.Exists(new_value))
                                new_value = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), new_value);
                        }

                        if (!File.Exists(new_value))
                            throw new FileNotFoundException(new_value);
                        else
                        {
                            hSource.Document = new HelpDocument(new_value, true, false);
                            hSource.DocumentProvider = new HelpDocumentProvider(hSource.Document);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }
            }
        }
    }
}
